<?php

include('functions.php');
set_time_limit(0);
ini_set('memory_limit','12M');
$runtime= new runtime;
$runtime->start();


include './phpquery/phpQuery/phpQuery.php'; 
//phpQuery::$debug = true;


/*$doc = phpQuery::newDocumentFile('test.html'); 
$value = pq($doc);
//var_dump($value);exit;

$td_count = pq($value)->find("td")->length;
$preg_number = '/(.*)-/isU';

$key = 0;*/

/*//供应商型号
$data[$key][sup_part] = pq($value)->find("td:eq(1)")->find("a")->html();

//制造商型号
$data[$key][part] = pq($value)->find("td:eq(2)")->find("a span")->html();

//描述
$data[$key][part] = pq($value)->find("td:eq(3)")->html();

//制造商
$data[$key][mfg] = pq($value)->find("td:eq(5)")->find("span a span")->html();

//数量
$desc_number[$key] = $td_count-4;
$data[$key][number] = pq($value)->find("td:eq($desc_number[$key])")->html();
$preg = preg_match($preg_number,$data[$key][number],$number[$key]);
$data[$key][number] = trim(str_ireplace(array('-',',','，'),'',$number[$key][1]));

//最少订购量
$desc_least[$key] = $td_count-3;
$data[$key][least_buy] = pq($value)->find("td:eq($desc_least[$key])")->html();
$data[$key][least_buy] = str_ireplace(array(',','，'),'',$data[$key][least_buy]);

//单价
$desc_price[$key] = $td_count-2;
$data[$key][price] = pq($value)->find("td:eq($desc_price[$key])")->find("a")->html();
$data[$key][price] = str_ireplace(array(',','，'),'',$data[$key][price]);*/





//var_dump($data[0]);exit;


$url = "http://www.digikey.cn/product-search/zh?x=0&y=0&lang=zh&site=cn&KeyWords=74hc00";
phpQuery::newDocumentFile($url);

$all_tr = pq("[itemtype='http://schema.org/Product']");
$td_count = count($all_tr->html());
var_dump($td_count);
exit;

//$td_count = pq("[itemtype='http://schema.org/Product']")->length();
var_dump($td_count);exit;
$preg_number = '/(.*)-/isU';
$key = 0;
foreach($all_tr as $value){
    
    //var_dump($value);exit;
    var_dump(pq($value)->html());exit;
    
    //供应商型号
    $data[$key][sup_part] = pq($value)->find("td:eq(1)")->find("a")->html();
    
    //制造商型号
    $data[$key][part] = pq($value)->find("td:eq(2)")->find("a span")->html();
    
    //描述
    $data[$key][part] = pq($value)->find("td:eq(3)")->html();
    
    //制造商
    $data[$key][mfg] = pq($value)->find("td:eq(5)")->find("span a span")->html();
    
    //数量
    $desc_number[$key] = $td_count-4;
    $data[$key][number] = pq($value)->find("td:eq($desc_number[$key])")->html();
    $preg = preg_match($preg_number,$data[$key][number],$number[$key]);
    $data[$key][number] = trim(str_ireplace(array('-',',','，'),'',$number[$key][1]));
    
    //最少订购量
    $desc_least[$key] = $td_count-3;
    $data[$key][least_buy] = pq($value)->find("td:eq($desc_least[$key])")->html();
    $data[$key][least_buy] = str_ireplace(array(',','，'),'',$data[$key][least_buy]);
    
    //单价
    $desc_price[$key] = $td_count-2;
    $data[$key][price] = pq($value)->find("td:eq($desc_price[$key])")->find("a")->html();
    $data[$key][price] = str_ireplace(array(',','，'),'',$data[$key][price]);
    var_dump($data);
    exit;
}





$runtime->stop();
echo "页面执行时间: ".$runtime->spent()." 毫秒";

phpQuery::$documents = array();
//清空数组防止内存过大
?>